package cc.xiaoxiya.yy;

import cc.xiaoxiya.yy.opt.text.OptBiqudao;
import cc.xiaoxiya.yy.opt.text.OptStart;
import cc.xiaoxiya.yy.opt.text.TextAction;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import utils.FileOpt;
import utils.HttpClientUtils;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;


/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws Exception
    {
    	//下载一个网页的内容 先获取整个页面，再按获取规则进行内容提取。
    	//根据一个uri获取一个页面，再根据提取规则进行提取。
//    	TextAction textAction = new TextAction("http://www.biqudao.com/bqge24946/1790665.html","d://a//",true);
//        OptStart optStart = new OptBiqudao();
//        optStart.start(textAction);

        //http://www.biqudao.com/bqge24946/
        TextAction textAction = new TextAction("http://www.biqudao.com/bqge24946/","d://a//",true);
        String html = HttpClientUtils.doGet(textAction.getUri(), null);
        Document doc = Jsoup.parse(html);
        Element element = doc.body().select("#list").select("dl").first();
        textAction.setName(doc.title().substring(0,doc.title().indexOf("最新")));
        Elements elist = element.children();
        List<TextAction> list = new ArrayList<TextAction>();
        int dt = 0;
        String host = "http://"+new URI(textAction.getUri()).getHost();
        for (int i = 1; i < elist.size() ; i++) {
            Element e1 = elist.get(i);
            if("dt".equals(e1.nodeName()) ){
                dt++;
            }else if(dt>=1 && "dd".equals(e1.nodeName())){
                Element e2 = e1.child(0);
                list.add( new TextAction(host+e2.attr("href"),e2.html(),textAction.getDir()+textAction.getName()+"//",true) );
            }
        }
        textAction.setContent(list.toString());
        textAction.setFlag(true);
        if (textAction.getFlag()) {
            FileOpt.writer(textAction.getContent(), textAction.getDir(), textAction.getName() + "-目录.json");
        }
    }
}
